package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class Languaje 
        extends
		Action
{
    
    public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		boolean cambio = MasterCardDAO.changeLanguaje( user.getCveUsuario( ) ) ;
                
                int actual = user.getIdioma( ) ;
        
        if( cambio ) {
            System.out.println( "(*_*)== Cambio de Idioma." ) ;
            if( actual == 0 ) {
                user.setIdioma( 1 ) ;
            } else if( actual == 1 )
            {
                user.setIdioma( 0 ) ;
            }
            
        } else {
            System.out.println( "(-_-)== No Change." ) ;
        }
        
        MasterCardDAO.closeEntityManager( ) ;
        return mapping.findForward( "success" ) ;
	}
    
}
